//
//  DJDetailCommentCollectionViewCell.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/7.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "DJDetailCommentCollectionViewCell.h"
#import "DJDetailCommentTableViewCell.h"
#import "CFMacro.h"
#import "UIView+Frame.h"
#import "DJDetailComment.h"
#import "Comment.h"
#import "Reply.h"

#define kDJDetailCommentTableViewCell @"DJDetailCommentTableViewCell"

@interface DJDetailCommentCollectionViewCell () <UITableViewDelegate, UITableViewDataSource>

// tableView
@property (weak, nonatomic) IBOutlet UITableView *tableView;
// view when no data
@property (nonatomic, strong) UILabel *noCommentLabel;
// cell height
@property (nonatomic, assign) CGFloat cellHeight;
// cell height array
@property (nonatomic, strong) NSMutableArray<NSString *> *cellHeights;

@property (nonatomic, strong) NSMutableArray<NSDictionary *> *subViewsHeights;
@end

@implementation DJDetailCommentCollectionViewCell

- (void)setRootComment:(DJDetailComment *)rootComment {
    _rootComment = rootComment;
    // 传入数据后计算每个cell的高度
    [self calculateCellHeight];
    // 移除
    [_noCommentLabel removeFromSuperview];
    // 创建tableView
    [self setUpTableView];
    [self.tableView reloadData];
}
#pragma mark - awake
- (void)awakeFromNib {
    [super awakeFromNib];
    // view when no data
    [self createNoCommentView];
    _tableView.tableFooterView = [UIView new];
    if (_rootComment == nil) {
    }
}
#pragma mark - 暂无评论
- (void)createNoCommentView {
    self.noCommentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.contentView.gx_height)];
    _noCommentLabel.text = @"暂无评论, 马上抢沙发";
    _noCommentLabel.font = [UIFont systemFontOfSize:12];
    _noCommentLabel.textColor = [UIColor lightGrayColor];
    _noCommentLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_noCommentLabel];
}
#pragma mark - 创建tableView
- (void)setUpTableView {
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 注册cell
    [_tableView registerClass:[DJDetailCommentTableViewCell class] forCellReuseIdentifier:kDJDetailCommentTableViewCell];
    _tableView.tableFooterView = [UIView new];
}

#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_rootComment) {
        return _rootComment.comment.count;
    } else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_rootComment) {
//        DJDetailCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDJDetailCommentTableViewCell];
        DJDetailCommentTableViewCell *cell = [[DJDetailCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kDJDetailCommentTableViewCell];
        cell.comment = _rootComment.comment[indexPath.row];
        cell.subViewsHeights = _subViewsHeights[indexPath.row];
        return cell;
    } else {
        return nil;
    }
}
#pragma mark - cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_rootComment) {
        CGFloat h = _cellHeights[indexPath.row].floatValue;
        h = h > 80 ? h : 80;
        return h;
    } else {
        return 80;
    }
}
#pragma mark - 计算每个cell的高度
- (void)calculateCellHeight {
    self.subViewsHeights = [NSMutableArray array];
    CGFloat commentH = 0;
    // 用来存放每个cell计算后的高度
    // 需要加上一些间距
    self.cellHeights = [NSMutableArray array];
    for (Comment *comment in _rootComment.comment) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        // 计算用户评论文字的高度
#warning 这里的宽度和字体都是固定值, 待解决
        commentH = [self selfSizeHeightWithString:comment.comment_content width:CGRectGetWidth([UIScreen mainScreen].bounds) - 60 - 30 font:kFont11];
        [dictionary setObject:[NSString stringWithFormat:@"%lf", commentH] forKey:@"commentH"];
        // 如果有他人回复
        if (comment.reply) {
            CGFloat replyH = 0;
            // 遍历所有他人的回复
            NSMutableArray *replys = [NSMutableArray array];
            for (Reply *reply in comment.reply) {
                // 拼接回复
                // comment_from_user_name 回复 comment_comment_to_user_name :comment_content
                NSString *replyStr = [NSString stringWithFormat:@"%@ 回复 %@ :%@", reply.reply_from_user_name, reply.reply_comment_to_user_name, reply.reply_content];
                CGFloat height = [self selfSizeHeightWithString:replyStr width:CGRectGetWidth([UIScreen mainScreen].bounds) - 60 - 30 font:kFont11];
                [replys addObject:[NSString stringWithFormat:@"%lf", height]];
                replyH += height;
            }
            [dictionary setObject:replys forKey:@"replys"];
            [_subViewsHeights addObject:dictionary];
            // 评论高度加上所有回复的高度
            if (replyH != 0) {
                commentH += replyH + 11 + 50;
            } else {
                commentH += 50;
            }
        }
        [_cellHeights addObject:[NSString stringWithFormat:@"%lf", ceil(commentH)]];
    }
}
#pragma mark - 计算矩形高度
- (CGFloat)selfSizeHeightWithString:(NSString *)string
                              width:(CGFloat)width
                               font:(UIFont *)font {
    CGRect newRect = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName : font} context:nil];
    return newRect.size.height + 0.5;
}

@end
